/*
 *  Copyright 2011 yura.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.vcforms.view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.Box;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.opu.vcforms.bean.ApplicationData;
import org.opu.vcforms.bean.EventBus;
import org.opu.vcforms.bean.model.ModelAvailableSource;
import org.opu.vcforms.view.component.GUIProperties;
import org.opu.vcforms.view.component.doublelist.JDoubleList;

/**
 *
 * @author yura
 */
public class ApplicationContent extends JPanel implements View{
    private ApplicationData data;
    private EventBus events;

    private JComboBox templates;
    private JComboBox semestrs;
    private JComboBox faculties;
    private JComboBox courses;
    private JDoubleList groups;

    private JLabel semestrLbl;
    private JLabel facultiesLbl;
    private JLabel coursesLbl;

    private ApplicationContentI18n i18n;

    public ApplicationContent(ApplicationData data, EventBus events,
            ApplicationContentI18n i18n) {
        super(new BorderLayout());
        this.data = data;
        this.events = events;
        this.i18n = i18n;
        ModelAvailableSource source = data.getSource();

        templates = new JComboBox(source.getTemplates());
        semestrs = new JComboBox(source.getSemestrs());
        faculties = new JComboBox(source.getFaculties());
        courses = new JComboBox(source.getCourses());
        groups = new JDoubleList(source.getGroups());

        semestrLbl = new JLabel();
        facultiesLbl = new JLabel();
        coursesLbl = new JLabel();

        initComponent();

        source.addViews(this);

        faculties.addActionListener(data.getDataChangeListener());
        courses.addActionListener(data.getDataChangeListener());

//        System.err.println(getClass().getName() + " line-" + 59);
//        faculties.setSelectedIndex(5);
    }

    @Override
    public void reinit() {
        removeAll();
        initComponent();
    }

    @Override
    public void resetData() {

    }

    public void setTexts(){
        boolean b = GUIProperties.instance().pane.isLabel();
        if (b) {
            semestrLbl.setText(i18n.getSemestrsLabel());
            coursesLbl.setText(i18n.getCoursesLabel());
            facultiesLbl.setText(i18n.getFacultiesLabel());
        } else {
            semestrLbl.setText(null);
            coursesLbl.setText(null);
            facultiesLbl.setText(null);
        }
    }

    private void setSizes(){
        Dimension d = getCompSize(null, semestrLbl);
        d = getCompSize(d, coursesLbl);
        d = getCompSize(d, facultiesLbl);
        setCompSize(d, semestrLbl, coursesLbl, facultiesLbl);

        d = getCompSize(null, semestrs);
        d = getCompSize(d, courses);
        d = getCompSize(d, faculties);
        d.width += 20;
        setCompSize(d, semestrs, courses, faculties);

        setCompSize(templates.getUI().getPreferredSize(templates), templates);
    }

    private void initComponent() {
        setTexts();
        setSizes();

        JComponent hbox1 = componentInHBoxGlue(templates);
        JComponent hbox2 = componentInHBox(semestrLbl, semestrs);
        JComponent hbox3 = componentInHBox(facultiesLbl, faculties);
        JComponent hbox4 = componentInHBox(coursesLbl, courses);
        JComponent hbox5 = groups;//componentInHBoxGlue(groups);


        Box V = Box.createVerticalBox();
        if (GUIProperties.instance().pane.isMaxGroupList()) {
            V.add(Box.createVerticalStrut(10));
            V.add(hbox1);
            V.add(Box.createVerticalStrut(10));
            V.add(hbox2);
            V.add(Box.createVerticalStrut(10));
            V.add(hbox3);
            V.add(Box.createVerticalStrut(10));
            V.add(hbox4);
            V.add(Box.createVerticalStrut(10));
            add(ViewHandler.getCompInStrut(V), BorderLayout.NORTH);
            add(ViewHandler.getCompInStrut(hbox5));
        } else {
            V.add(Box.createVerticalStrut(10));
            V.add(hbox1);
            V.add(Box.createGlue());
            V.add(Box.createGlue());
            V.add(hbox2);
            V.add(Box.createGlue());
            V.add(hbox3);
            V.add(Box.createGlue());
            V.add(hbox4);
            V.add(Box.createGlue());
            V.add(Box.createGlue());
            V.add(hbox5);
            V.add(Box.createGlue());
            add(ViewHandler.getCompInStrut(V));
        }
    }

    JComponent componentInHBox(JLabel lbl, JComponent comp){
        Box box = Box.createHorizontalBox();
        box.add(Box.createHorizontalStrut(2));
        if (lbl != null && GUIProperties.instance().pane.isLabel()) {
            box.add(lbl);
            box.add(Box.createHorizontalStrut(4));
        }
        box.add(comp);
        box.add(Box.createGlue());
        return box;
    }

    JComponent componentInHBoxGlue(JComponent comp){
        Box box = Box.createHorizontalBox();
        box.add(Box.createGlue());
        box.add(comp);
        box.add(Box.createGlue());
        return box;
    }

    Dimension getCompSize(Dimension d, JComponent c){
        return getCompSize(d, c, c.getPreferredSize());
    }

    Dimension getCompSize(Dimension d, JComponent c, Dimension max){
        if (d == null){
            d = c.getPreferredSize();
        }

        Dimension d2 = c.getPreferredSize();

        if (d.width < d2.width){
            d.width = d2.width;
        }

        if (d.width > max.width){
            d.width = max.width;
        }

        if (d.height < d2.height){
            d.height = d2.height;
        }

        if (d.height > max.height){
            d.height = max.height;
        }

        return d;
    }

    void setCompSize(Dimension d, JComponent... comp){
        for (int i = 0; i < comp.length; i++) {
            comp[i].setPreferredSize(d);
            comp[i].setMinimumSize(d);
            comp[i].setMaximumSize(d);
        }
    }

}
